package com.component.api.model.dto;

import cn.hutool.core.util.StrUtil;
import com.component.api.function.CompareFunction;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 描述：
 * 校验规则列
 * @author xianggj
 * @Date 2021/11/29 13:44
 **/
@Data
@NoArgsConstructor
public class ValidationColumnDTO extends ColumnDTO implements Serializable {


    private static final long serialVersionUID = -1L;

    /**
     * 规则
     */
    public static final String NEGATIVE = "negative";
    public static final String EMPTY = "empty";
    private String rule;


    /**
     * 获取计算公式
     * @return
     */
    public CompareFunction getFunction(){
        if (StrUtil.isBlank(rule)) {
            return (a,b) -> false;
        } else {
            return (a,b) ->{
                if (StrUtil.contains(rule, EMPTY)){
                    if (a == null) {
                        return true;
                    }
                    if (StrUtil.isBlank(a.toString())){
                        return true;
                    }
                }
                if (StrUtil.contains(rule, NEGATIVE)){
                    if (a == null) {
                        return false;
                    }
                    if (StrUtil.startWith(a.toString(), '-')){
                        return true;
                    }
                }
                return false;
            };
        }
    }
}
